
const fs = require('fs');
const http = require('http')

const server = http.createServer((req,res)=>{
    let {pathname} = new URL(req.url, 'http://localhost:3001')


    if(pathname === '/'){
        pathname = '/index.html'
    }
    const getDir = __dirname;
    console.log(__dirname);
    const filePath = getDir + pathname;

    fs.readFile(filePath,(err,data)=>{
        if(err){
            res.end('<h1>404 Not Found</h1>');
        } else
         res.end(data)
    })

})


server.listen(3001,()=>{
    console.log('启动成功','http://localhost:3001');
})